<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <style></style>
  </head>
  <body>
    <div>
      <h1>惰性函数与性能</h1>
    </div>
    <button id="btn"></button>
    <script>
      var btn = document.getElementById("btn");
      function foo() {
        console.log(this);
      }

      function addEvent(obj, type, fn) {
        if (obj.addEventListener) {
          obj.addEventListener(type, fn, false);
        } else if (obj.attachEvebt) {
          obj.attachEvent("on" + type, fn);
        } else {
          obj["on" + type] = fn;
        }
      }

      //使用惰性函数----结果比上面的代码慢了非常多(本案例)
      function addEvent(obj, type, fn) {
        if (obj.addEventListener) {
          addEvent = obj.addEventListener(type, fn, false);
        } else if (obj.attachEvebt) {
          addEvent = obj.attachEvent("on" + type, fn);
        } else {
          addEvent = obj["on" + type] = fn;
        }
        return addEvent;
      }
      addEvent(btn, "click", foo);
    </script>
  </body>
</html>
